package com.xt.wikiperfumapi.model.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xt.wikiperfumapi.model.OrderInfo;
import com.xt.wikiperfumapi.model.OrderPerfumeRef;
import com.xt.wikiperfumapi.model.PerfumeInfo;
import com.xt.wikiperfumapi.model.vo.OrderPerfumeRefVo;
import com.xt.wikiperfumapi.model.vo.OrderVo;
import com.xt.wikiperfumapi.model.vo.PerfumeVo;
import com.xt.wikiperfumapi.service.impl.OrderInfoServiceImpl;
import com.xt.wikiperfumapi.service.impl.OrderPerfumeRefServiceImpl;
import com.xt.wikiperfumapi.service.impl.PerfumeInfoServiceImpl;
import com.xt.wikiperfumapi.service.impl.UserInfoServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class OrderWrapper {

//    @Resource
//    OrderInfoServiceImpl orderInfoService;

    @Resource
    OrderPerfumeRefServiceImpl orderPerfumeRefService;

    @Resource
    UserInfoServiceImpl userInfoService;

    @Resource
    PerfumeInfoServiceImpl perfumeInfoService;

    public OrderVo orderToVo(OrderInfo orderInfo){
        OrderVo orderVo=new OrderVo();
        try {
            BeanUtils.copyProperties(orderVo,orderInfo);
        }catch (Exception e){
            log.info(e.getMessage());
        }
        List<OrderPerfumeRef> orderPerfumeRefs = orderPerfumeRefService.list(new QueryWrapper<OrderPerfumeRef>()
                .eq("order_id", orderInfo.getOrderId())
                .eq("is_deleted",0)
        );
        //获取这个订单的所有的香水信息
        List<OrderPerfumeRefVo> orderPerfumeRefVos=new ArrayList<>();
        orderPerfumeRefs.forEach(el->{
            OrderPerfumeRefVo orderPerfumeRefVo1=new OrderPerfumeRefVo();
            try {
                BeanUtils.copyProperties(orderPerfumeRefVo1,el);
            }catch (Exception e){
                log.info(e.getMessage());
            }
            PerfumeVo perfumeVo=new PerfumeVo();
            PerfumeInfo byId = perfumeInfoService.getById(el.getPerfumeId());
            try {
                BeanUtils.copyProperties(perfumeVo,byId);
            }catch (Exception e){
                log.info(e.getMessage());
            }
            orderPerfumeRefVo1.setPerfumeVo(perfumeVo);
            orderPerfumeRefVos.add(orderPerfumeRefVo1);
        });
        orderVo.setOrderPerfumeRefVos(orderPerfumeRefVos);
        orderVo.setUserInfo(userInfoService.getById(orderInfo.getUserId()));

        return orderVo;
    }

}
